﻿using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using B.MESUI.Login.A_Public.Model;
using Newtonsoft.Json;

public class CodeGenApiClient
{
    private readonly string _baseUrl;
    private readonly HttpClient _client;

    public CodeGenApiClient()
    {
        _baseUrl = "http://39.103.49.30:9983";
        _client = new HttpClient();
    }

    public async Task<string> GenerateCode(string ruleName)
    {
        var url = $"{_baseUrl}/api/Code/generate";
        var reqObj = new { ruleName = ruleName };
        var json = JsonConvert.SerializeObject(reqObj);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var resp = await _client.PostAsync(url, content);
        resp.EnsureSuccessStatusCode();
        var respStr = await resp.Content.ReadAsStringAsync();
        dynamic obj = JsonConvert.DeserializeObject(respStr);
        return obj.code;
    }

    public async Task<bool> CreateRule(string ruleName, string prefix, string connector, int serialLength)
    {
        var url = $"{_baseUrl}/api/Code/rule";
        var reqObj = new { ruleName = ruleName, prefix = prefix, connector = connector, serialLength = serialLength };
        var json = JsonConvert.SerializeObject(reqObj);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var resp = await _client.PostAsync(url, content);
        var respStr = await resp.Content.ReadAsStringAsync();
        dynamic obj = JsonConvert.DeserializeObject(respStr);
        return obj.success == true;
    }

    public async Task<List<RuleViewModel>> GetAllRules()
    {
        var url = $"{_baseUrl}/api/Code/rules";
        var resp = await _client.GetAsync(url);
        resp.EnsureSuccessStatusCode();
        var respStr = await resp.Content.ReadAsStringAsync();
        dynamic obj = JsonConvert.DeserializeObject(respStr);
        var rules = new List<RuleViewModel>();
        foreach (var r in obj.rules)
        {
            rules.Add(new RuleViewModel
            {
                ruleName = r.ruleName,
                prefix = r.prefix,
                connector = r.connector,
                serialLength = r.serialLength
            });
        }
        return rules;
    }

    public async Task<RuleViewModel> GetRuleDetail(string ruleName)
    {
        var url = $"{_baseUrl}/api/Code/rule/detail";
        var reqObj = new { ruleName = ruleName };
        var json = JsonConvert.SerializeObject(reqObj);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var resp = await _client.PostAsync(url, content);
        resp.EnsureSuccessStatusCode();
        var respStr = await resp.Content.ReadAsStringAsync();
        dynamic obj = JsonConvert.DeserializeObject(respStr);
        if (obj == null || obj.rule == null)
            return null;
        var rule = obj.rule;
        return new RuleViewModel
        {
            ruleName = rule.ruleName,
            prefix = rule.prefix,
            connector = rule.connector,
            serialLength = rule.serialLength
        };
    }
    /// <summary>
    /// 预览生成的编码
    /// </summary>
    /// <param name="ruleName">规则名称</param>
    /// <returns>预览的编码</returns>
    public async Task<string> PreviewCode(string ruleName)
    {
        var url = $"{_baseUrl}/api/Code/preview";
        var reqObj = new { ruleName = ruleName };
        var json = JsonConvert.SerializeObject(reqObj);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var resp = await _client.PostAsync(url, content);
        resp.EnsureSuccessStatusCode();
        var respStr = await resp.Content.ReadAsStringAsync();
        dynamic obj = JsonConvert.DeserializeObject(respStr);
        return obj.code;
    }

}